package cj.web.sc.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.constants.MemuEnum;
import cj.web.constants.SexEnum;
import cj.web.sc.dao.MemberDAO;
import cj.web.sc.domain.Member;
import cj.web.sc.domain.SCCategory;

@Path("/buyer")
public class MemberController{

	private static Logger logger = Logger.getLogger(MemberController.class.getName());
	
	@Autowired
	private MemberDAO buyerDao;

	@Get("show")
	public String show(Model model,SCCategory sCCategory)
	{
		model.add("MemuEnum", MemuEnum.hyg);
		return "/views/sc/buyerList.vm";
	}
	
	@Post("list")
	public String list(Model model,Member buyer)
	{
		List<Member> objs = buyerDao.queryByPhone(buyer);
		
		for(Member obj:objs){
			if(obj.getSex()==null){
				obj.setSexName("");
			}else{
				obj.setSexName(SexEnum.getName(obj.getSex()));
			}
		}
		int size = buyerDao.sizeByPhone(buyer);
		
		JSONObject jo = new JSONObject();
		jo.put("data", objs);
		jo.put("draw", buyer.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("unbundling")
	public String unbundling(@Param("buyerId")int buyerId)
	{
		buyerDao.unbundlingByUserId(buyerId);
		return "r:show";
	}
}
